$(function(){

	$("#various1").fancybox({
		'titlePosition': 'inside',
		'transitionIn': 'none',
		'transitionOut': 'none'
	});

	$("#picCut").live("click",function(){
		var top = $("#picCut").css("top");
		var left  =$("#picCut").css("left");
		if(top[0]>0 || left[0]>0){
			$(".movetext").css("display","none");
			$(".imageCut .submit").css("display","block");
		}
	});

	$("#publishedrecipe-add-edit .imageCut .submit").click(function(){

		var bigpic = $("#picSeeBigInfor").val();
		var smallpic = $("#picSeeSmallInfor").val();
		var orgname = $("#recipepic").val();
	//	$.post(APP+'/Recipe/cutimg',{picSeeSmallInfor:smallpic,picSeeBigInfor:bigpic,image:orgname},function(data){
		$.post(APP+'/JsonData/cutimg',{picSeeSmallInfor:smallpic,picSeeBigInfor:bigpic,image:orgname},function(data){
		//	var obj = JSON.parse(data);
		//	$("#publishedrecipe-add-edit .right .img img").attr("src",PUBLIC+'/Content/tmp/Big_'+obj[1]);
			$("#publishedrecipe-add-edit .right .img img").attr("src",PUBLIC+'/Content/tmp/Big_'+data);
			$("#publishedrecipe-add-edit .left .recipepic").val(data);
		});
		$("#fancybox-close").fancybox().trigger('click');

	});

	$("#publishedrecipe-add-edit .calculation").find("input").toggle(function(){
		var firstamount = $(".left [name='mainamount[]']:first").val();
		var firstmain = $(".left [name='main[]']:first").val();
		if(firstmain=='' || firstamount=='' || firstamount==0){
			window.scrollTo(0,365);
			alert("请选择先主料");
			return false;
		}
		$(this).bind('click',getdatas());
		$(".calculate").css("display","none");
	//	$(".calculate ul").html('');
		$(".calculate").fadeIn();
		window.scrollTo(0,365);
	},function(){
		var firstamount = $(".left [name='mainamount[]']:first").val();
		var firstmain = $(".left [name='main[]']:first").val();
		if(firstmain=='' || firstamount=='' || firstamount==0){
			window.scrollTo(0,365);
			alert("请选择先主料");
			return false;
		}
		$(this).bind('click',getdatas());
		$(".calculate").css("display","none");
	//	$(".calculate ul").html('');
		$(".calculate").fadeIn();
		window.scrollTo(0,365);
	});

	function getdatas(){
		var mainamount = new Array();
		var maindeal = new Array();
		var mainsectionid = new Array();
		var maincategoryid = new Array();
		var mainarticleid = new Array();
		$("#publishedrecipe-add-edit .left [name='mainamount[]']").each(function(index){
			var maina = $(this);
			mainamount[index] = maina.val();
		});
		$("#publishedrecipe-add-edit .left [name='maindeal[]']").each(function(index){
			var maind = $(this);
			maindeal[index] = maind.val();
		});
		$("#publishedrecipe-add-edit .left [name='mainfoodsectionid[]']").each(function(index){
			var mains = $(this);
			mainsectionid[index] = mains.val();
		});
		$("#publishedrecipe-add-edit .left [name='mainfoodcategoryid[]']").each(function(index){
			var mainc = $(this);
			maincategoryid[index] = mainc.val();
		});
		$("#publishedrecipe-add-edit .left [name='mainfoodarticleid[]']").each(function(index){
			var mainar = $(this);
			mainarticleid[index] = mainar.val();
		});

		var accessoryamount = new Array();
		var accessorydeal = new Array();
		var accessorysectionid = new Array();
		var accessorycategoryid = new Array();
		var accessoryarticleid = new Array();
		$("#publishedrecipe-add-edit .left [name='accessoryamount[]']").each(function(index){
			var accessorya = $(this);
			accessoryamount[index] = accessorya.val();
		});
		$("#publishedrecipe-add-edit .left [name='accessorydeal[]']").each(function(index){
			var accessoryd = $(this);
			accessorydeal[index] = accessoryd.val();
		});
		$("#publishedrecipe-add-edit .left [name='accessoryfoodsectionid[]']").each(function(index){
			var accessorys = $(this);
			accessorysectionid[index] = accessorys.val();
		});
		$("#publishedrecipe-add-edit .left [name='accessoryfoodcategoryid[]']").each(function(index){
			var accessoryc = $(this);
			accessorycategoryid[index] = accessoryc.val();
		});
		$("#publishedrecipe-add-edit .left [name='accessoryfoodarticleid[]']").each(function(index){
			var accessoryar = $(this);
			accessoryarticleid[index] = accessoryar.val();
		});

		var condimentamount = new Array();
		var condimentdeal = new Array();
		var condimentsectionid = new Array();
		var condimentcategoryid = new Array();
		var condimentarticleid = new Array();
		$("#publishedrecipe-add-edit .left [name='condimentamount[]']").each(function(index){
			var condimenta = $(this);
			condimentamount[index] = condimenta.val();
		});
		$("#publishedrecipe-add-edit .left [name='condimentdeal[]']").each(function(index){
			var condimentd = $(this);
			condimentdeal[index] = condimentd.val();
		});
		$("#publishedrecipe-add-edit .left [name='condimentfoodsectionid[]']").each(function(index){
			var condiments = $(this);
			condimentsectionid[index] = condiments.val();
		});
		$("#publishedrecipe-add-edit .left [name='condimentfoodcategoryid[]']").each(function(index){
			var condimentc = $(this);
			condimentcategoryid[index] = condimentc.val();
		});
		$("#publishedrecipe-add-edit .left [name='condimentfoodarticleid[]']").each(function(index){
			var condimentar = $(this);
			condimentarticleid[index] = condimentar.val();
		});

	//	$.post(APP+'/Recipe/calculate',{
		$.post(APP+'/JsonData/calculate',{
			mainamount:mainamount,
			maindeal:maindeal,
			mainsectionid:mainsectionid,
			maincategoryid:maincategoryid,
			mainarticleid:mainarticleid,
			accessoryamount:accessoryamount,
			accessorydeal:accessorydeal,
			accessorysectionid:accessorysectionid,
			accessorycategoryid:accessorycategoryid,
			accessoryarticleid:accessoryarticleid,
			condimentamount:condimentamount,
			condimentdeal:condimentdeal,
			condimentsectionid:condimentsectionid,
			condimentcategoryid:condimentcategoryid,
			condimentarticleid:condimentarticleid
		},function(result){
			var obj = JSON.parse(result);
			$(".calculate ul li").each(function(index){
				var c=$(this);
				c.find(".spandata").html(obj[index]);
			});
//			$(".calculate ul").html(result);
		});
	}

	$("#publishedrecipe-add-edit [name='mainamount[]']").css("background-color","#EBEBE4");
	$("#publishedrecipe-add-edit [name='maindeal[]']").css("background-color","#EBEBE4");
	$("#publishedrecipe-add-edit [name='mainpreparation[]']").css("background-color","#EBEBE4");
	$("#publishedrecipe-add-edit [name='mainamount[]']").attr("disabled","disabled");
	$("#publishedrecipe-add-edit [name='maindeal[]']").attr("disabled","disabled");
	$("#publishedrecipe-add-edit [name='mainpreparation[]']").attr("disabled","disabled");

	$("#publishedrecipe-add-edit [name='accessoryamount[]']").css("background-color","#EBEBE4");
	$("#publishedrecipe-add-edit [name='accessorydeal[]']").css("background-color","#EBEBE4");
	$("#publishedrecipe-add-edit [name='accessorypreparation[]']").css("background-color","#EBEBE4");
	$("#publishedrecipe-add-edit [name='accessoryamount[]']").attr("disabled","disabled");
	$("#publishedrecipe-add-edit [name='accessorydeal[]']").attr("disabled","disabled");
	$("#publishedrecipe-add-edit [name='accessorypreparation[]']").attr("disabled","disabled");

	$("#publishedrecipe-add-edit [name='condimentamount[]']").css("background-color","#EBEBE4");
	$("#publishedrecipe-add-edit [name='condimentdeal[]']").css("background-color","#EBEBE4");
	$("#publishedrecipe-add-edit [name='condimentpreparation[]']").css("background-color","#EBEBE4");
	$("#publishedrecipe-add-edit [name='condimentamount[]']").attr("disabled","disabled");
	$("#publishedrecipe-add-edit [name='condimentdeal[]']").attr("disabled","disabled");
	$("#publishedrecipe-add-edit [name='condimentpreparation[]']").attr("disabled","disabled");

	$("#publishedrecipe-add-edit .left .addmain").click(function(){
		var orghtml = $("#addmaintothis");
		var newhtml = $("#waitmain").html();
		$(newhtml).appendTo($(orghtml));
		$("#publishedrecipe-add-edit .right").height($("#publishedrecipe-add-edit .left").height());
	});

	$("#publishedrecipe-add-edit .left .maindelete").live("click",function(){
		var num = $("#publishedrecipe-add-edit .left .maindelete").size();
		var main = $(this).parents('.maindls');
		if(num>1){
			main.remove();
			$("#publishedrecipe-add-edit .right").height($("#publishedrecipe-add-edit .left").height());
		}else{
			alert('只有一个不能删除');
		}
	});

	$("#publishedrecipe-add-edit .left .addother").click(function(){
		var orghtml = $("#addothertothis");
		var newhtml = $("#waitother").html();
		$(newhtml).appendTo($(orghtml));
		$("#publishedrecipe-add-edit .right").height($("#publishedrecipe-add-edit .left").height());
	});

	$("#publishedrecipe-add-edit .left .otherdelete").live("click",function(){
		var num = $("#publishedrecipe-add-edit .left .otherdelete").size();
		var main = $(this).parents('.otherdls');
		var newhtml = $("#waitother").html();
		if(num>1){
			main.remove();
			$("#publishedrecipe-add-edit .right").height($("#publishedrecipe-add-edit .left").height());
		}else{
			main.replaceWith(newhtml);
		}
	});

	$("#publishedrecipe-add-edit .left .addothers").click(function(){
		var orghtml = $("#addotherstothis");
		var newhtml = $("#waitothers").html();
		$(newhtml).appendTo($(orghtml));
		$("#publishedrecipe-add-edit .right").height($("#publishedrecipe-add-edit .left").height());
	});

	$("#publishedrecipe-add-edit .left .othersdelete").live("click",function(){
		var num = $("#publishedrecipe-add-edit .left .othersdelete").size();
		var main = $(this).parents('.othersdls');
		var newhtml = $("#waitothers").html();
		if(num>1){
			main.remove();
			$("#publishedrecipe-add-edit .right").height($("#publishedrecipe-add-edit .left").height());
		}else{
			main.replaceWith(newhtml);
		}
	});


	$(".left [name='mainamount[]']").live("keyup",function(){

		var v=$(this);
		if(isNaN(v.val())){
			v.css("background-color","yellow");
		}else if(v.val()==0){
			v.css("background-color","yellow");
		}else if(v.val()==''){
			v.css("background-color","white");
		}else{
			v.css("background-color","white");
		}

	});

	$(".left [name='accessoryamount[]']").live("keyup",function(){
		var v=$(this);
		if(isNaN(v.val())){
			v.css("background-color","yellow");
		}else if(v.val()==0){
			v.css("background-color","yellow");
		}else if(v.val()==''){
			v.css("background-color","white");
		}else{
			v.css("background-color","white");
		}
	});

	$(".left [name='condimentamount[]']").live("keyup",function(){
		var v=$(this);
		if(isNaN(v.val())){
			v.css("background-color","yellow");
		}else if(v.val()==0){
			v.css("background-color","yellow");
		}else if(v.val()==''){
			v.css("background-color","white");
		}else{
			v.css("background-color","white");
		}
	});

	$(".left [name='mainamount[]']").each(function(){

		var v=$(this);
		if(v.val()!='' && !isNaN(v.val()) && v.val()!=0){
			var mid = v.parents(".maindls").find("[name='maindealid']").val();

			var sid = v.parents(".maindls").find("[name='mainfoodsectionid[]']").val();
			var cid = v.parents(".maindls").find("[name='mainfoodcategoryid[]']").val();
			var id = v.parents(".maindls").find("[name='mainfoodarticleid[]']").val();
		//	$.post(APP+'/Recipe/getdeals', {sectionid:sid,categoryid:cid,articleid:id},function(maindatas){
			$.post(APP+'/JsonData/getdeals', {sectionid:sid,categoryid:cid,articleid:id},function(maindatas){
				var obj = JSON.parse(maindatas);
				var org = v.parents(".maindls").find("[name='maindeal[]']");
				org.html('');
				for(var i=0; i<obj.length; i++){
					if(obj[i].unit==mid){
						$('<option selected="selected" title="'+obj[i].deal+'g/'+obj[i]['setdeal']['name']+'" value="'+obj[i].unit+'">'+obj[i]['setdeal']['name']+'</option>').appendTo(org);
					}else{
						$('<option title="'+obj[i].deal+'g/'+obj[i]['setdeal']['name']+'" value="'+obj[i].unit+'">'+obj[i]['setdeal']['name']+'</option>').appendTo(org);
					}
				}
			});
		}
	});

	$(".left [name='accessoryamount[]']").each(function(){

		var v=$(this);
		if(v.val()!='' && !isNaN(v.val()) && v.val()!=0){
			var mid = v.parents(".otherdls").find("[name='accessorydealid']").val();

			var sid = v.parents(".otherdls").find("[name='accessoryfoodsectionid[]']").val();

			var cid = v.parents(".otherdls").find("[name='accessoryfoodcategoryid[]']").val();
			var id = v.parents(".otherdls").find("[name='accessoryfoodarticleid[]']").val();

		//	$.post(APP+'/Recipe/getdeals', {sectionid:sid,categoryid:cid,articleid:id},function(accessorydatas){
			$.post(APP+'/JsonData/getdeals', {sectionid:sid,categoryid:cid,articleid:id},function(accessorydatas){

				var obj = JSON.parse(accessorydatas);

				var org = v.parents(".otherdls").find("[name='accessorydeal[]']");

				org.html('');
				for(var i=0; i<obj.length; i++){
					if(obj[i].unit==mid){

						$('<option selected="selected" title="'+obj[i].deal+'g/'+obj[i]['setdeal']['name']+'" value="'+obj[i].unit+'">'+obj[i]['setdeal']['name']+'</option>').appendTo(org);
					}else{

						$('<option title="'+obj[i].deal+'g/'+obj[i]['setdeal']['name']+'" value="'+obj[i].unit+'">'+obj[i]['setdeal']['name']+'</option>').appendTo(org);
					}
				}

			});
		}
	});

	$(".left [name='condimentamount[]']").each(function(){

		var v=$(this);
		if(v.val()!='' && !isNaN(v.val()) && v.val()!=0){
			var mid = v.parents(".othersdls").find("[name='condimentdealid']").val();

			var sid = v.parents(".othersdls").find("[name='condimentfoodsectionid[]']").val();

			var cid = v.parents(".othersdls").find("[name='condimentfoodcategoryid[]']").val();
			var id = v.parents(".othersdls").find("[name='condimentfoodarticleid[]']").val();

		//	$.post(APP+'/Recipe/getdeals', {sectionid:sid,categoryid:cid,articleid:id},function(condimentdatas){
			$.post(APP+'/JsonData/getdeals', {sectionid:sid,categoryid:cid,articleid:id},function(condimentdatas){

				var obj = JSON.parse(condimentdatas);

				var org = v.parents(".othersdls").find("[name='condimentdeal[]']");
				org.html('');
				for(var i=0; i<obj.length; i++){
					if(obj[i].unit==mid){

						$('<option selected="selected" title="'+obj[i].deal+'g/'+obj[i]['setdeal']['name']+'" value="'+obj[i].unit+'">'+obj[i]['setdeal']['name']+'</option>').appendTo(org);
					}else{

						$('<option title="'+obj[i].deal+'g/'+obj[i]['setdeal']['name']+'" value="'+obj[i].unit+'">'+obj[i]['setdeal']['name']+'</option>').appendTo(org);
					}
				}

			});
		}
	});

	$("#recipeform").submit(function(){

		if($("#checkbox").attr("checked")!='checked'){
			alert("您未同意本站协议条款");
			return false;
		}

		$(".left [name='mainamount[]']:enabled").each(function(){

			var v=$(this);

			if(v.val()=='' || isNaN(v.val()) || v.val()==0){
				v.addClass("addcolor");
				na = 0;
				return false;
			}else{
				na = 1;
			}

		});

		$(".left [name='accessoryamount[]']:enabled").each(function(){

			var v=$(this);

			if(v.val()=='' || isNaN(v.val()) || v.val()==0){
				nb = 0;
				return false;
			}else{
				nb = 1;
			}

		});

		$(".left [name='condimentamount[]']:enabled").each(function(){

			var v=$(this);

			if(v.val()=='' || isNaN(v.val()) || v.val()==0){
				v.addClass("addcolor");
				nc = 0;
				return false;
			}else{
				nc = 1;
			}

		});

		var chooseubtypeid = $("#publishedrecipe-add-edit #chooseubtype").val();

		 var recipename = $("#publishedrecipe-add-edit .recipename").val();

		var count = $('.word_count1').html();

		var firstamount = $(".left [name='mainamount[]']:first").val();

		var firstmain = $(".left [name='main[]']:first").val();

		if(recipename==''){
			$("#publishedrecipe-add-edit .recipename").css("background-color","yellow");
			window.scrollTo(0,0);
			alert("Recipe name can't for empty");
			return false;
		}else if(count<1){
			alert("Recipe details can't for empty");
			return false;
		}else if(chooseubtypeid==0){
			$("#publishedrecipe-add-edit #chooseubtype").css("background-color","yellow");
			window.scrollTo(0,0);
			alert('Please Choose article type');
			return false;
		}else if(firstmain=='' || firstmain==0){
			window.scrollTo(0,365);
			alert('第一个主料为必填');
			$(".left [name='main[]']:first").css("background-color","yellow");
			return false;
		}else if(firstamount=='' || firstamount==0 || isNaN(firstamount)){
			window.scrollTo(0,365);
			alert('数量不能为零或为空,必须为数字');
			return false;
		}else if(na==0){
			window.scrollTo(0,365);
			alert('数量不能为零或为空,必须为数字');
			return false;
		}else if(nb==0){
			window.scrollTo(0,365);
			alert('数量不能为零或为空,必须为数字');
			return false;
		}else if(nc==0){
			window.scrollTo(0,365);
			alert('数量不能为零或为空,必须为数字');
			return false;
		}

	});

	$("#publishedrecipe-add-edit .recipename").keyup(function(){
		$(this).css("background-color","white");
	});


	$("#selecttypearticle").change(function(){

		var v = $(this).val();

		if(v==1){

		//	$.post(APP+'/Recipe/returnrecipecategorys', function(data){
			$.post(APP+'/JsonData/returnrecipecategorys', function(data){
				var obj = JSON.parse(data);
				var subtype = $("#publishedrecipe-add-edit #chooseubtype");
				subtype.html("");
				$('<option value="0">-- Choose Subclasses --</option>').appendTo(subtype);
				for(var i=0; i<obj.length; i++){
					$('<option value="'+obj[i].id+'">'+obj[i].name+'</option>').appendTo(subtype);
				}
			});

			$("#recipeform").attr("action",APP+'/Recipe/insert');
		//	alert($("#recipeform").attr("action"));

			$("#course").find("select").removeAttr("disabled");
			$("#course").prev(".entrymyself").css("visibility","visible");

			$("#occasion").find("select").removeAttr("disabled");
			$("#occasion").prev(".entrymyself").css("visibility","visible");

			$("#dietaryneeds").find("select").removeAttr("disabled");
			$("#dietaryneeds").prev(".entrymyself").css("visibility","visible");

			$("#servepeople").find("select").removeAttr("disabled");
			$("#servepeople").prev(".entrymyself").css("visibility","visible");

			$("#totaltime").find("select").removeAttr("disabled");
			$("#totaltime").prev(".entrymyself").css("visibility","visible");

			$("#publishedrecipe-add-edit .whentips").css("display","block");
			$("#publishedrecipe-add-edit .right").height($("#publishedrecipe-add-edit .left").height());

			$("#publishedrecipe-add-edit [name='recipetype']").val("Recipe");

		}else if(v==2){

		//	$.post(APP+'/Recipe/returnspecialcategorys', function(data){
			$.post(APP+'/JsonData/returnspecialcategorys', function(data){
				var obj = JSON.parse(data);
				var subtype = $("#publishedrecipe-add-edit #chooseubtype");
				subtype.html("");
				$('<option value="0">-- Choose Subclasses --</option>').appendTo(subtype);
				for(var i=0; i<obj.length; i++){
					$('<option value="'+obj[i].id+'">'+obj[i].name+'</option>').appendTo(subtype);
				}
			});

			$("#recipeform").attr("action",APP+'/Special/insert');
		//	alert($("#recipeform").attr("action"));

			$("#course").find("select").removeAttr("disabled");
			$("#course").prev(".entrymyself").css("visibility","visible");

			$("#occasion").find("select").removeAttr("disabled");
			$("#occasion").prev(".entrymyself").css("visibility","visible");

			$("#dietaryneeds").find("select").removeAttr("disabled");
			$("#dietaryneeds").prev(".entrymyself").css("visibility","visible");

			$("#servepeople").find("select").removeAttr("disabled");
			$("#servepeople").prev(".entrymyself").css("visibility","visible");

			$("#totaltime").find("select").removeAttr("disabled");
			$("#totaltime").prev(".entrymyself").css("visibility","visible");

			$("#publishedrecipe-add-edit .whentips").css("display","block");
			$("#publishedrecipe-add-edit .right").height($("#publishedrecipe-add-edit .left").height());

			$("#publishedrecipe-add-edit [name='recipetype']").val("Special");

		}else if(v==3){

		//	$.post(APP+'/Recipe/returnsaucecategorys', function(data){
			$.post(APP+'/JsonData/returnsaucecategorys', function(data){
				var obj = JSON.parse(data);
				var subtype = $("#publishedrecipe-add-edit #chooseubtype");
				subtype.html("");
				$('<option value="0">-- Choose Subclasses --</option>').appendTo(subtype);
				for(var i=0; i<obj.length; i++){
					$('<option value="'+obj[i].id+'">'+obj[i].name+'</option>').appendTo(subtype);
				}
			});

			$("#recipeform").attr("action",APP+'/Sauce/insert');
		//	alert($("#recipeform").attr("action"));

			$("#totaltime").find("select").removeAttr("disabled");
			$("#totaltime").prev(".entrymyself").css("visibility","visible");

			$("#course").find("select").attr("disabled","disabled");
			$("#course").prev(".entrymyself").css("visibility","hidden");

			$("#occasion").find("select").attr("disabled","disabled");
			$("#occasion").prev(".entrymyself").css("visibility","hidden");

			$("#dietaryneeds").find("select").attr("disabled","disabled");
			$("#dietaryneeds").prev(".entrymyself").css("visibility","hidden");

			$("#servepeople").find("select").attr("disabled","disabled");
			$("#servepeople").prev(".entrymyself").css("visibility","hidden");

			$("#publishedrecipe-add-edit [name='recipetype']").val("Sauce");

		}else if(v==4){

		//	$.post(APP+'/Recipe/returnkitchencategorys', function(data){
			$.post(APP+'/JsonData/returnkitchencategorys', function(data){
				var obj = JSON.parse(data);
				var subtype = $("#publishedrecipe-add-edit #chooseubtype");
				subtype.html("");
				$('<option value="0">-- Choose Subclasses --</option>').appendTo(subtype);
				for(var i=0; i<obj.length; i++){
					$('<option value="'+obj[i].id+'">'+obj[i].name+'</option>').appendTo(subtype);
				}
			});

			$("#recipeform").attr("action",APP+'/Kitchen/insert');
			$("#publishedrecipe-add-edit .whentips").css("display","none");
			$("#publishedrecipe-add-edit .right").height($("#publishedrecipe-add-edit .left").height());

			$("#course").find("select").attr("disabled","disabled");
			$("#course").prev(".entrymyself").css("visibility","hidden");

			$("#occasion").find("select").attr("disabled","disabled");
			$("#occasion").prev(".entrymyself").css("visibility","hidden");

			$("#dietaryneeds").find("select").attr("disabled","disabled");
			$("#dietaryneeds").prev(".entrymyself").css("visibility","hidden");

			$("#servepeople").find("select").attr("disabled","disabled");
			$("#servepeople").prev(".entrymyself").css("visibility","hidden");

			$("#totaltime").find("select").attr("disabled","disabled");
			$("#totaltime").prev(".entrymyself").css("visibility","hidden");

		//	alert($("#recipeform").attr("action"));

			$("#publishedrecipe-add-edit [name='recipetype']").val("Kitchen");
		}

	});

	$("#chooseubtype").change(function(){
		$(this).css("background-color","white");
	});

	$("[name='main[]']").each(function(){

		var names = $(this);
		if(names.val()!==''){
			names.parents(".maindls").find("[name='mainpreparation[]']").css("background-color","white");
			names.parents(".maindls").find("[name='mainpreparation[]']").removeAttr("disabled");
			names.parents(".maindls").find("[name='mainamount[]']").css("background-color","white");
			names.parents(".maindls").find("[name='mainamount[]']").removeAttr("disabled");
			names.parents(".maindls").find("[name='maindeal[]']").css("background-color","white");
			names.parents(".maindls").find("[name='maindeal[]']").removeAttr("disabled");
		}

	});

	$("[name='other[]']").each(function(){

		var names = $(this);
		if(names.val()!==''){
			names.parents(".otherdls").find("[name='accessorypreparation[]']").css("background-color","white");
			names.parents(".otherdls").find("[name='accessorypreparation[]']").removeAttr("disabled");
			names.parents(".otherdls").find("[name='accessoryamount[]']").css("background-color","white");
			names.parents(".otherdls").find("[name='accessoryamount[]']").removeAttr("disabled");
			names.parents(".otherdls").find("[name='accessorydeal[]']").css("background-color","white");
			names.parents(".otherdls").find("[name='accessorydeal[]']").removeAttr("disabled");
		}

	});

	$("[name='condiment[]']").each(function(){

		var names = $(this);
		if(names.val()!==''){
			names.parents(".othersdls").find("[name='condimentpreparation[]']").css("background-color","white");
			names.parents(".othersdls").find("[name='condimentpreparation[]']").removeAttr("disabled");
			names.parents(".othersdls").find("[name='condimentamount[]']").css("background-color","white");
			names.parents(".othersdls").find("[name='condimentamount[]']").removeAttr("disabled");
			names.parents(".othersdls").find("[name='condimentdeal[]']").css("background-color","white");
			names.parents(".othersdls").find("[name='condimentdeal[]']").removeAttr("disabled");
		}

	});

	$("[name='main[]']").live('focus',function(){
		var names = $(this);
		$("#publishedrecipe-add-edit").removeClass(".query");
		names.addClass(".query");
		var options = {
		//	serviceUrl:APP+'/Recipe/getmains',//获取数据的后台页面
			serviceUrl:APP+'/JsonData/getmains',//获取数据的后台页面
			width: 200,//提示框的宽度
			delimiter: /(,|;)\s*/,//分隔符
//			onSelect: onAutocompleteSelect,//选中之后的回调函数
			onSelect: function(value, data){

				if(data==0){
					names.val('');
					names.css("background-color","yellow");
				}else{
					var splits = data.split('|');
					names.parents(".maindls").find("[name='mainfoodsectionid[]']").val(splits[0]);
					names.parents(".maindls").find("[name='mainfoodcategoryid[]']").val(splits[1]);
					names.parents(".maindls").find("[name='mainfoodarticleid[]']").val(splits[2]);
					names.css("background-color","white");
				}

			//	$.post(APP+'/Recipe/getdeals', {sectionid:splits[0],categoryid:splits[1],articleid:splits[2]},function(datas){
				$.post(APP+'/JsonData/getdeals', {sectionid:splits[0],categoryid:splits[1],articleid:splits[2]},function(datas){
					var obj = JSON.parse(datas);
					var org = names.parents(".maindls").find("[name='maindeal[]']");
					org.html('');
					for(var i=0; i<obj.length; i++){
						$('<option title="'+obj[i].deal+'g/'+obj[i]['setdeal']['name']+'" value="'+obj[i].unit+'">'+obj[i]['setdeal']['name']+'</option>').appendTo(org);
					}
					names.parents(".maindls").find("[name='mainamount[]']").val(1);
					names.parents(".maindls").find("[name='mainpreparation[]']").css("background-color","white");
					names.parents(".maindls").find("[name='mainpreparation[]']").removeAttr("disabled");
					names.parents(".maindls").find("[name='mainamount[]']").css("background-color","white");
					names.parents(".maindls").find("[name='mainamount[]']").removeAttr("disabled");
					names.parents(".maindls").find("[name='maindeal[]']").css("background-color","white");
					names.parents(".maindls").find("[name='maindeal[]']").removeAttr("disabled");
				});
			},
			deferRequestBy: 0, //单位微秒
//			params: { country: 'Yes' },//参数
			noCache: false //是否启用缓存 默认是开启缓存的
//			lookup: ['January', 'Februarys','Februaryf','Februaryg','Februarys','Februaryg','Februaryg','Februarye','Februarye', 'March', 'April', 'May'] //local lookup values
		};
		names.autocomplete(options);
	});

	$("[name='main[]']").live('blur',function(){

		var name = $(this);
		if(name.val()==''){
			name.val('');
			name.css("background-color","yellow");
		}

//		if(name.parents(".maindls").find("[name='mainfoodarticleid[]']").val()=='' || name.parents(".maindls").find("[name='mainfoodarticleid[]']").val()==0){
//			name.parents(".maindls").find("[name='mainfoodarticleid[]']").val(0);
//			name.val('');
//			name.css("background-color","yellow");
//		}
	});

	$("[name='other[]']").live('focus',function(){
		var names = $(this);
		$("#publishedrecipe-add-edit").removeClass(".query");
		names.addClass(".query");
		var options = {
		//	serviceUrl:APP+'/Recipe/getothers',//获取数据的后台页面
			serviceUrl:APP+'/JsonData/getothers',//获取数据的后台页面
			width: 200,//提示框的宽度
			delimiter: /(,|;)\s*/,//分隔符
//			onSelect: onAutocompleteSelect,//选中之后的回调函数
			onSelect: function(value, data){
			//	names.parents(".otherdls").find("[name='othervalue[]']").val(data);
			//	alert(names.parents(".otherdls").find("[name='othervalue[]']").val());
				var splits = data.split('|');
				if(data==0){
					names.val('');
					names.css("background-color","yellow");
				}else{

					names.parents(".otherdls").find("[name='accessoryfoodsectionid[]']").val(splits[0]);
					names.parents(".otherdls").find("[name='accessoryfoodcategoryid[]']").val(splits[1]);
					names.parents(".otherdls").find("[name='accessoryfoodarticleid[]']").val(splits[2]);
					names.css("background-color","white");

				}

			//	$.post(APP+'/Recipe/getdeals', {sectionid:splits[0],categoryid:splits[1],articleid:splits[2]},function(datas){
				$.post(APP+'/JsonData/getdeals', {sectionid:splits[0],categoryid:splits[1],articleid:splits[2]},function(datas){
					var obj = JSON.parse(datas);
					var org = names.parents(".otherdls").find("[name='accessorydeal[]']");
					org.html('');
					for(var i=0; i<obj.length; i++){
						$('<option title="'+obj[i].deal+'g/'+obj[i]['setdeal']['name']+'" value="'+obj[i].unit+'">'+obj[i]['setdeal']['name']+'</option>').appendTo(org);
					}

					names.parents(".otherdls").find("[name='accessoryamount[]']").val(1);
					names.parents(".otherdls").find("[name='accessoryamount[]']").css("background-color","white");
					names.parents(".otherdls").find("[name='accessoryamount[]']").removeAttr("disabled");
					names.parents(".otherdls").find("[name='accessorydeal[]']").css("background-color","white");
					names.parents(".otherdls").find("[name='accessorydeal[]']").removeAttr("disabled");
					names.parents(".otherdls").find("[name='accessorypreparation[]']").css("background-color","white");
					names.parents(".otherdls").find("[name='accessorypreparation[]']").removeAttr("disabled");
				});
			},
			deferRequestBy: 0, //单位微秒
//			params: { country: 'Yes' },//参数
			noCache: false //是否启用缓存 默认是开启缓存的
//			lookup: ['January', 'Februarys','Februaryf','Februaryg','Februarys','Februaryg','Februaryg','Februarye','Februarye', 'March', 'April', 'May'] //local lookup values
		};
		names.autocomplete(options);
	});

	$("[name='other[]']").live('blur',function(){
		var name = $(this);
		if(name.parents(".otherdls").find("[name='accessoryfoodarticleid[]']").val()=='' || name.parents(".otherdls").find("[name='accessoryfoodarticleid[]']").val()==0){
			name.parents(".otherdls").find("[name='accessoryfoodarticleid[]']").val(0);
			name.val('');
			name.css("background-color","yellow");
		}
	});

	$("[name='condiment[]']").live('focus',function(){
		var names = $(this);
		$("#publishedrecipe-add-edit").removeClass(".query");
		names.addClass(".query");
		var options = {
		//	serviceUrl:APP+'/Recipe/condiments',//获取数据的后台页面
			serviceUrl:APP+'/JsonData/condiments',//获取数据的后台页面
			width: 200,//提示框的宽度
			delimiter: /(,|;)\s*/,//分隔符
//			onSelect: onAutocompleteSelect,//选中之后的回调函数
			onSelect: function(value, data){
				var splits = data.split('|');
				if(data==0){
					names.val('');
					names.css("background-color","yellow");
				}else{
					names.parents(".othersdls").find("[name='condimentfoodsectionid[]']").val(splits[0]);
					names.parents(".othersdls").find("[name='condimentfoodcategoryid[]']").val(splits[1]);
					names.parents(".othersdls").find("[name='condimentfoodarticleid[]']").val(splits[2]);
					names.css("background-color","white");
				}
			//	$.post(APP+'/Recipe/getdeals', {sectionid:splits[0],categoryid:splits[1],articleid:splits[2]},function(datas){
				$.post(APP+'/JsonData/getdeals', {sectionid:splits[0],categoryid:splits[1],articleid:splits[2]},function(datas){
					var obj = JSON.parse(datas);
					var org = names.parents(".othersdls").find("[name='condimentdeal[]']");
					org.html('');
					for(var i=0; i<obj.length; i++){
						$('<option title="'+obj[i].deal+'g/'+obj[i]['setdeal']['name']+'" value="'+obj[i].unit+'">'+obj[i]['setdeal']['name']+'</option>').appendTo(org);
					}
					names.parents(".othersdls").find("[name='condimentamount[]']").val(1);
					names.parents(".othersdls").find("[name='condimentamount[]']").css("background-color","white");
					names.parents(".othersdls").find("[name='condimentamount[]']").removeAttr("disabled");
					names.parents(".othersdls").find("[name='condimentdeal[]']").css("background-color","white");
					names.parents(".othersdls").find("[name='condimentdeal[]']").removeAttr("disabled");
					names.parents(".othersdls").find("[name='condimentpreparation[]']").css("background-color","white");
					names.parents(".othersdls").find("[name='condimentpreparation[]']").removeAttr("disabled");

				});
			//	alert(names.parents(".othersdls").find("[name='condimentvalue[]']").val());
			},
			deferRequestBy: 0, //单位微秒
//			params: { country: 'Yes' },//参数
			noCache: false //是否启用缓存 默认是开启缓存的
//			lookup: ['January', 'Februarys','Februaryf','Februaryg','Februarys','Februaryg','Februaryg','Februarye','Februarye', 'March', 'April', 'May'] //local lookup values
		};
		names.autocomplete(options);
	});

	$("[name='condiment[]']").live('blur',function(){
		var name = $(this);
		if(name.parents(".othersdls").find("[name='condimentfoodarticleid[]']").val()=='' || name.parents(".othersdls").find("[name='condimentfoodarticleid[]']").val()==0){
			name.parents(".othersdls").find("[name='condimentfoodarticleid[]']").val(0);
			name.val('');
			name.css("background-color","yellow");
		}
	});

	$("#publishedrecipe-add-edit .entrymyself").toggle(function(){
		var a = $(this);
		var yes = a.next("span").children().is("input");
		var id = a.next("span").attr("id");
		var inputdata = a.next("span").html();
		$("#waitdata").data("datas",inputdata);
		var newselect = $("#waitselect").find("."+id).html();
		a.next("span").html('');
		if(yes){ //第一次点击判断刚开始是否是input
			//如果是就显示下拉列表
			a.next("span").html(newselect);
		}else{
			//如果不是的就显示输入框
			a.next("span").html('<input class="newinput" type="text" name="'+id+'">');
		}

	},function(){
		var a = $(this);
		var id = a.next("span").attr("id");
		var yes = a.next("span").children().is("select");
		var newselect = $("#waitselect").find("."+id).html();
		a.next("span").html('');
		if(yes){
			//第二次点击,如果是输入框
		//	a.next("span").html('<input class="newinput" type="text" name="'+id+'"');
			a.next("span").html($("#waitdata").data("datas"));
		}else{
			a.next("span").html(newselect);
		}


	});

	$("#publishedrecipe-add-edit .right").height($("#publishedrecipe-add-edit .left").height()+10);

	var text = $("#publishedrecipe-add-edit .recipepic");

	var button = $("#uploadphoto");

	var fileType = "pic",fileNum = "one";

	new AjaxUpload(button,{
	//	action: APP+'/Recipe/uploads',
		action: APP+'/JsonData/uploads',
		name: 'pic',
		onSubmit : function(file, ext){
			if(fileType == "image")
			{
				if (ext && /^(jpg|png|jpeg|gif)$/.test(ext)){
					this.setData({
						'info': '文件类型为图片'
					});
				} else {
					alert('非图片类型文件，请重传');
					return false;
				}
			}
		},
		onComplete: function(file, response){
			if(isNaN(response[0])){
				alert(response);
			}else{
			//	text.val(response);
//				$("#various3").fancybox().trigger('click');
				$("#various1").fancybox().trigger('click');
				$("#various1").bind("click",cutImage());

				$("#picDiv").find("img").attr("src",PUBLIC+'/Content/tmp/'+response);
				$("#picSeeSmall").find("img").attr("src",PUBLIC+'/Content/tmp/'+response);
				$("#picSeeBig").find("img").attr("src",PUBLIC+'/Content/tmp/'+response);

				$(".imageCut .submit").css("display","none");
				$(".movetext").css("display","block");

			}
		}
	});

	function cutImage(){
		var pDiv=document.getElementById('picDiv');
		pDiv.innerHTML='<div id="picCut"><div id="picCutChange"></div></div>'+pDiv.innerHTML;
		var pDivImg=pDiv.getElementsByTagName('img')[0],
			clip=pDiv.getElementsByTagName('img')[1],
			pCut=document.getElementById('picCut'),
			pChange=document.getElementById('picCutChange');
		pSeeS=document.getElementById('picSeeSmall'),
			pSee=document.getElementById('picSeeBig'),
			pDivW=pDiv.offsetWidth-2,
			pDivH=pDiv.offsetHeight-2,
			opacity=function(obj,v){
				if(!-[1,]){obj.style.filter='alpha(opacity='+v+')';};
				obj.style.MozOpacity=v/100;
				obj.style.opacity=v/100;
			},
			see=function(){
				var getPos={
					't':pCut.offsetTop,
					"l":pCut.offsetLeft,
					'w':pCut.offsetWidth,
					'h':pCut.offsetHeight,
					'kw':(pDivImg.width-pDivW)/Math.max(1,pDivW-pCut.offsetWidth),
					'kh':(pDivImg.height-pDivH)/Math.max(1,pDivH-pCut.offsetHeight)
				};
				init(getPos);
				seePic(pSee,getPos);
				seePic(pSeeS,getPos);
				window.getSelection?window.getSelection().removeAllRanges():document.selection.empty();
			},
			init=function(getPos){
				var t=getPos.t*getPos.kh,
					l=getPos.l*getPos.kw;
				if(!clip){
					clip=document.createElement('img');
					clip.src=pDivImg.src;
					pDiv.appendChild(clip);
				}
				pDivImg.style.cssText='top:-'+t+'px; left:-'+l+'px';
				clip.style.cssText='top:-'+t+'px; left:-'+l+'px';

				clip.style.clip='rect('+(t+getPos.t)+'px '+(l+getPos.l+getPos.w)+'px '+(t+getPos.t+getPos.h)+'px '+(l+getPos.l)+'px)';
				opacity(pDivImg,50);
			},
			seePic=function(obj,getPos){
				var img=obj.getElementsByTagName('img')[0],
					k=obj.offsetWidth/getPos.w;
				t=getPos.t*getPos.kh+getPos.t,
					l=getPos.l*getPos.kw+getPos.l;
				if(!img){
					img=document.createElement('img');
					img.src=pDivImg.src;
					obj.appendChild(img);
				}
				img.style.cssText='width:'+k*pDivImg.width+'px; height:'+k*pDivImg.height+'px; position:absolute; top:-'+t*k+'px; left:-'+l*k+'px;';
				document.getElementById(obj.id+'Infor').value="{'x':'"+l+"','y':'"+t+"','w':'"+getPos.w+"','h':'"+getPos.h+"','k':'"+k+"','nw':'"+(obj.offsetWidth-2)+"','nh':'"+(obj.offsetHeight-2)+"','url':'"+pDivImg.src+"'}";
			},
			clear=function(o){
				if(!-[1,]){o.setCapture();}//学增加丢失焦点事件
				document.onmouseup=function(){
					if(!-[1,]){o.releaseCapture();}
					document.onmousemove=null;
					document.onmouseup=null;
				};
				return false;//webkit中去掉默认事件,使鼠标不会变成默认文本光标
			};
		if(!!window.ActiveXObject){//-[1,]已经不能判断ie9了
			var cDiv=document.createElement('div');
			cDiv.style.cssText='width:100%; height:100%; background:#fff; opacity:0; filter:alpha(opacity=0); font-size:0;';
			pCut.appendChild(cDiv);
		}
		pCut.onmousedown=function(e){
			e=e||window.event;
			var oldX=e.clientX-pCut.offsetLeft,
				oldY=e.clientY-pCut.offsetTop,
				maxW=pDivW-pCut.offsetWidth,
				maxH=pDivH-pCut.offsetHeight;
			document.onmousemove=function(e){
				e=e||window.event;
				var newX=e.clientX-oldX,newY=e.clientY-oldY;
				newX=newX<0?0:newX>maxW?maxW:newX;
				newY=newY<0?0:newY>maxH?maxH:newY;
				pCut.style.top=newY+'px';
				pCut.style.left=newX+'px';
				see();
			};
			clear(this);
		};
		pChange.onmousedown=function(e){
			e=e||window.event;
			var oldX=e.clientX,
				oldY=e.clientY,
				oldT=pCut.offsetTop+2,
				oldL=pCut.offsetLeft+2,
				oldW=pCut.offsetWidth,
				oldH=pCut.offsetHeight,
				minW=50,minH=50;
			if(e.stopPropagation){
				e.stopPropagation();
			}else{
				e.cancelBubble=true;
			}
			document.onmousemove=function(e){
				e=e||window.event;
				var w=e.clientX-oldX+oldW;
				w=w<minW?minW:w>pDivW-oldL?pDivW-oldL:w;
				h=w<minH?minH:w>pDivH-oldT?pDivH-oldT:w;
				w=w!=h?h:w;
				pCut.style.width=w+'px';
				pCut.style.height=h+'px';
				if(!-[1,]&&!window.XMLHttpRequest){cDiv.style.height=h+'px';}//ie6下高度不变bug
				see();
			};
			clear(this);
		};
		see();
	}

});

